package com.ipangyang.aidldemo.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;

import com.ipangyang.aidldemo.ipclient.Book;
import com.ipangyang.aidldemo.ipclient.BookManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by arthur on 2017/9/4.
 */
public class AidlService extends Service {
    private static final String TAG = AidlService.class.getName();
    private List<Book> books = new ArrayList<>();

    private BookManager.Stub bookManager = new BookManager.Stub(){

        @Override
        public List<Book> listAll() throws RemoteException {
            synchronized (this){
                return books;
            }

        }

        @Override
        public int getBookCount() throws RemoteException {
            synchronized (this) {
                return books.size();
            }
        }

        @Override
        public Book addBookIn(Book book) throws RemoteException {
            synchronized (this){
                Log.d(TAG, "addBookIn: 价格+1");
                book.setPrice(book.getPrice() + 1);
                book.setName(book.getName()+"in");
                books.add(book);
                return book;
            }

        }

        @Override
        public Book addBookOut(Book book) throws RemoteException {
            synchronized (this) {
                Log.d(TAG, "addBookOut: 价格+2");
                book.setPrice(book.getPrice()+2);
                book.setName(book.getName()+"out");
                books.add(book);
                return book;
            }
        }

        @Override
        public Book addBookInout(Book book) throws RemoteException {
            synchronized (this) {
                Log.d(TAG, "addBookInOut: 价格+3");
                book.setPrice(book.getPrice()+3);
                book.setName(book.getName()+"inout");
                books.add(book);
                return book;
            }
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        Book book = new Book();
        book.setName("android 从入门到放弃");
        book.setPrice(19);
        books.add(book);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "onBind: ");
        return bookManager;
    }
}
